package com.jala.base;

import com.jala.base.data.WxSessionCache;
import com.jala.base.data.WxSessionMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Set;

public class HttpSessionWrapper implements HttpSession {
	private static final Log logger = LogFactory.getLog(com.jala.ecs.client.session.HttpSessionWrapper.class);
	private final ServletContext servletContext;
	private WxSessionMap wxSessionMap;
	private WxSessionCache wxSessionCache;
	private boolean invalidated;
	private boolean old;
	private int maxActiveTime;
	

	public HttpSessionWrapper(WxSessionMap wxSessionMap, WxSessionCache wxSessionCache, int maxActiveTime, ServletContext servletContext) {
		this.wxSessionMap = wxSessionMap;
		this.wxSessionCache = wxSessionCache;
		this.maxActiveTime = maxActiveTime;
		this.servletContext = servletContext;
	}
	
	@Override
	public void setMaxInactiveInterval(int interval) {
		wxSessionMap.setMaxInactiveInterval(interval);
		wxSessionCache.setMaxInactiveInterval(wxSessionMap.getId(), interval);
	}
	
	@Override
	public void setAttribute(String name, Object value) {
		wxSessionMap.setAttribute(name, value);
		wxSessionCache.put(wxSessionMap.getId(), wxSessionMap, maxActiveTime);
	}
	
	@Override
	public void removeAttribute(String name) {
		wxSessionMap.removeAttribute(name);
		wxSessionCache.put(wxSessionMap.getId(), wxSessionMap, maxActiveTime);
	}

	@Override
	public void putValue(String name, Object value) {
		setAttribute(name, value);
	}

	@Override
	public void removeValue(String name) {
		removeAttribute(name);
	}

	@Override
	public long getCreationTime() {
		return wxSessionMap.getCreationTime();
	}

	@Override
	public String getId() {
		return wxSessionMap.getId();
	}

	@Override
	public long getLastAccessedTime() {
		return wxSessionMap.getLastAccessedTime();
	}

	@Override
	public ServletContext getServletContext() {
		return servletContext;
	}

	
	@Override
	public int getMaxInactiveInterval() {
		return wxSessionMap.getMaxInactiveInterval();
	}


	@Override
	public Object getAttribute(String name) {
		return wxSessionMap.getAttribute(name);
	}

	@Override
	public Object getValue(String name) {
		return getAttribute(name);
	}

	@Override
	public Enumeration<String> getAttributeNames() {
		return Collections.enumeration(wxSessionMap.getAttributeNames());
	}

	@Override
	public String[] getValueNames() {
		Set<String> attrs = wxSessionMap.getAttributeNames();
		return attrs.toArray(new String[0]);
	}

	@Override
	public void invalidate() {
		this.invalidated = true;
		logger.debug("session invalidate,sessionid=" + wxSessionMap.getId());
		wxSessionCache.destroy(wxSessionMap.getId());
	}

	public void setNew(boolean isNew) {
		this.old = !isNew;
	}

	@Override
	public boolean isNew() {
		return !old;
	}

	@Override
	public HttpSessionContext getSessionContext() {
		return null;
	}


}
